<?php
defined('_JEXEC') or die( 'Restricted access' );

class TableHarvestRecord extends Fcore
{
	/** @var int Primary key */
	var $id					= 0;
	/** @var int */
	var $ppid				= 0;
	/** @var int */
	var $uid				= 0;
	/** @var datetime */
	var $harvest_date		= '0000-00-00';
	/** @var datetime */
	var $datetime			= '0000-00-00';
	/** @var string */
	var $production			= '';

	/**
	* @param database A database connector object
	*/
	function __construct( &$db )
	{
		parent::__construct( '#__farm_plant_harvest', 'id', $db );
	}

	/**
	 * Binds an array to the object
	 * @param 	array	Named array
	 * @param 	string	Space separated list of fields not to bind
	 * @return	boolean
	 */
	function bind( $array, $ignore='' )
	{
		$result = parent::bind( $array );
		// cast properties
		$this->id	= (int) $this->id;
		return $result;
	}

	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{
		if (!intval( $this->ppid ))
		{
			$this->setError(JText::_( '请选择农场' ));
			return false;
		}
		
		if (!intval( $this->uid ))
		{
			$this->setError(JText::_( '请选择记录人员' ));
			return false;
		}
		
		if (trim( $this->harvest_date ) == '')
		{
			$this->setError(JText::_( '请输入实际收割日' ));
			return false;
		}
		
		if (trim( $this->production ) == '')
		{
			$this->setError(JText::_( '请输入实际产量' ));
			return false;
		}
		
		if (trim( $this->datetime ) == '')
		{
			$this->datetime = date('Y-m-d');
		}
		return true;
	}
	
	public function hrlist()
	{
		$data = $this->alist();
		if (is_array($data[1]) && count($data[1])) {
			$t = JTable::getInstance('plantPlan', 'Table');
			foreach ($data[1] as $k => $v) {
				$data[1][$k]->pAfInfo	= $t->getInfo($v->ppid);
				$data[1][$k]->uName		= $this->uName($v->uid);
			}
		}
		return $data;
	}
	
	/**
	 * 获取用户姓名
	 * @param int $uid 
	 */
	public function uName($uid)
	{
		$tmpTable	= $this->_tbl;
		$tmpKey		= $this->_tbl_key;
		
		$this->_tbl		= "#__users";
		$this->_tbl_key	= 'id';
		
		$return = $this->getOne(intval($uid), 'name');
		
		$this->_tbl		= $tmpTable;
		$this->_tbl_key	= $tmpKey;
		
		return $return;
	}
}
